//
//  DetailVC.swift
//  StoreProject
//
//  Created by CYC on 2021/9/29.
//

import UIKit

class DetailVC: BaseViewController, UIScrollViewDelegate {
    
    var BW = CGFloat(0)
    var dataList = [YearModel]()            // 所有数据
    var offestList = [DetailOffestModel]()  // 每个月的位移
    var scrollView = UIScrollView()

    // MARK:======================================生命周期========================================
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        
        if true {
            
            let btn = UIButton.init(frame: CGRect.init(x: kScreenWidth - kSpaceSize - 100, y: kNavHeight - 44, width: 100, height: 44))
            btn.contentHorizontalAlignment = .right
            btn.imageView?.contentMode = .scaleAspectFit
            btn.c_setImage("13")
            btn.addTarget(self, action: #selector(button1Action(btn:)), for: .touchUpInside)
            self.navView!.addSubview(btn)
        }
        
    }
  

    func creatUIAction(list: [YearModel]) {
        
        
        
        self.dataList = list
        
        
        
        if true {
            
            self.navLine?.isHidden = true
            
            let view1 = UIView.init(frame: CGRect.init(x: 0, y: kNavHeight, width: kScreenWidth, height: 25))
            view1.backgroundColor = .white
            self.view.addSubview(view1)
            
            let line = UIView.init(frame: CGRect.init(x: 0, y: view1.c_height() - kLineSize, width: view1.c_width(), height: kLineSize))
            line.backgroundColor = kLineColor
            view1.addSubview(line)
            
            let titleList = ["日", "一", "二", "三", "四", "五", "六"]
            self.BW = CGFloat((view1.c_width() - kSpaceSize*2)/CGFloat(titleList.count))
            for index in 0..<titleList.count {
                
                let lab = UILabel.init(frame: CGRect.init(x: kSpaceSize + self.BW*CGFloat(index), y: 0, width: self.BW, height: view1.c_height()))
                lab.textAlignment = .center
                lab.font = kMFont(10)
                lab.text = titleList[index]
                view1.addSubview(lab)
                
                if index == 0 || index == 6 {
                    lab.textColor = kLabelColor2
                } else {
                    lab.textColor = kLabelColor1
                }
            }
        }
        
        
        let scrollView = UIScrollView.init(frame: CGRect.init(x: 0, y: kNavHeight + 25, width: kScreenWidth, height: kScreenHeight - kNavHeight - 25))
        scrollView.alwaysBounceVertical = true
        scrollView.alwaysBounceHorizontal = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.delegate = self
        self.view.addSubview(scrollView)
        self.scrollView = scrollView
        
        var height = CGFloat(0.0)
        
        for yearModel in self.dataList {
            
            
            for index1 in 0..<yearModel.monthList.count {
    
                let monthModel = yearModel.monthList[index1]

                // 记录该月份的区间起点
                let offestModel = DetailOffestModel()
                offestModel.month = monthModel
                offestModel.offestStartY = height
                            
                let view1 = DetailMonthView.init(frame: .init(x: 0, y: height, width: kScreenWidth, height: 50))
                view1.creatUIAction(model: monthModel)
                scrollView.addSubview(view1)
                height = view1.c_bottom()
                
                for index2 in 0..<monthModel.dayList.count {
                    
                    let dayModel = monthModel.dayList[index2]
                    let weekIndex = dayModel.date.weekday! - 1
                    let view2 = DetailDayView.init(frame: .init(x: kSpaceSize + BW * CGFloat(weekIndex), y: height, width: BW, height: 60))
                    view2.addTarget(self, action: #selector(button2Action(btn:)), for: .touchUpInside)
                    view2.creatUIAction(model: dayModel)
                    scrollView.addSubview(view2)
                    
                    if dayModel.date.weekday == 1 {
                        
                        // 如果是星期日，在左边加一行
                        let line = UIView.init(frame: CGRect.init(x: 0, y: view2.c_top(), width: kSpaceSize, height: kLineSize))
                        line.backgroundColor = kLineColor
                        scrollView.addSubview(line)
                    }
                    
                    if dayModel.date.weekday == 7 {
                        
                        // 如果是星期六，在右边加一行
                        let line = UIView.init(frame: CGRect.init(x: view2.c_right(), y: view2.c_top(), width: kSpaceSize, height: kLineSize))
                        line.backgroundColor = kLineColor
                        scrollView.addSubview(line)
                    }
                    
                    if weekIndex == 6 || index2 == monthModel.dayList.count - 1 {
                        
                        // 已经是这周6或最后一天，则该行结束
                        height = view2.c_bottom()
                    }
                    
                    if index2 == monthModel.dayList.count - 1 {
                        
                        // 记录该月份的区间终点
                        offestModel.offestEndY = height
                        self.offestList.append(offestModel)
                    }
                }
                
            }
            
        }
        
        scrollView.contentSize = CGSize.init(width: CGFloat(kScreenWidth), height: CGFloat(height + 50))
        self.scrollViewDidScroll(scrollView)    // 主动滑一下
        
    }
    
    // MARK:设置月份，进行精准位移
    func setMonthAction(model: MonthModel) {
        
        for item in self.offestList {
            
            if model.year == item.month.year && model.month == item.month.month {
                
                self.scrollView.setContentOffset(.init(x: 0, y: item.offestStartY), animated: false)
                return
            }
        }
    }
    
    
    // MARK:======================================点击响应========================================
    
    // MARK:点击添加
    @objc func button1Action(btn: UIButton) {
        
        self.navigationController?.pushViewController(AddNoticeVC(), animated: true)
    }
    
    // MARK:点击天
    @objc func button2Action(btn: DetailDayView) {
    
        for item in DateNoticeModel.getDatas() {
            
            if item.year.integerValue == btn.model.date.year &&
                item.month.integerValue == btn.model.date.month &&
                item.day.integerValue == btn.model.date.day
                {
                
                let vc = NoticeVC()
                vc.model = btn.model
                self.navigationController?.pushViewController(vc, animated: true)
                
                break
            }
        }

        
    }
    
    // MARK:======================================网络请求========================================
    
    
    
    // MARK:======================================代理方法========================================
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let offestY = scrollView.contentOffset.y
        for item in self.offestList {
            
            if offestY >= item.offestStartY && offestY < item.offestEndY {
                
                self.titleLabel?.text = String.init(format: "%d年%d月", item.month.year, item.month.month)
                return
            }
        }
        
    }
    
    
    
    


}
